<template>
	<view class="cartList">
		<view class="cart" v-for="(s,i) in datas" :key="i" @click="goprice(s.price)">
			<view class="check" >
				<!-- <checkbox-group @change='setChecked'> -->
					<label>
						<checkbox color="#a397fd" style="transform:scale(0.7)" :checked='checked'></checkbox>
					</label>
				<!-- </checkbox-group> -->
			</view>
			<view class="image">
				<image :src="$base+s.pic" mode="widthFix"></image>
			</view>
			<view class="text">
				<text class="title">{{s.title}}</text>
				<text class="price">¥{{s.price}}</text>
				<uni-tag text="包邮到家" type="warning" />
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		name:"cartList",
		props:['item','datas'],
		data() {
			return {
				checked: false,
				pricenum:0,
				num:0,
				index:1,
				boolActive: false ,
				nummm:0
				
			}
		},
		update() {
			this.checked=!this.item
			console.log(this.checked);
				},

		methods: {
			goprice(e){
				this.nummm +=e
				console.log(Math.ceil(this.nummm))
				this.$emit('showChild', {
					childName: Math.ceil(this.nummm)
				})
				
			},
			
			
			setChecked() {
				if (this.item === false) {
					this.checked = !this.checked
				 // console.log(this.$props.datas)
				 //循环遍历数组将所有价格相加
				 this.$props.datas.forEach((p,index)=>{
					 
					 // console.log(p.price);
				     this.index=index//数组个数
					 this.pricenum += p.price
					 this.num+=1 //相加次数
				 })
				// console.log(Math.ceil(this.pricenum)) 	
				// console.log(this.index+1);
				// console.log(this.num);
				//相加次数大于数组长度+1时执行下面
			    if(this.num>=this.index+1){
				   if(Math.trunc(this.num/this.index)<=2){
					   //如果数组相加次数除以数组长度加1那么价格就等于
					this.pricenum=Math.ceil(this.pricenum/(Math.trunc(this.num/this.index)))
                
				this.$emit('showChild', {
					childName: this.pricenum
				})
					// console.log(this.pricenum)
					}else{
						window.location.reload()
					}
					
					
					
					
				}else{
					this.pricenum=Math.ceil(this.pricenum)
				}
				
				
				
				} else {
					this.checked = !this.checked
					
				}
			},
// 		checkedVal(){
// 			uni.$emit('checked',this.checked)
// }
		}
	}
</script>

<style lang="scss">
	.cartList{
		:first-child{
			margin: 0 auto;
		}
	}
	.cart {
		display: flex;
		width: 85%;
		border: 2px solid #a397fd;
		border-radius: 50rpx;
		padding: 30rpx;
		margin:10rpx auto;
		// position: relative;
		// top: -100rpx;
		z-index: 999;
		background-color: #fff;
		
		.image {
			width: 20%;

			image {
				width: 100%;
			}
		}

		.text {
			width: 60%;

			.title {
				display: block;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;

			}

			.price {
				display: block;
				color: #a397fd;
				font-weight: bold;
			}

			.num {
				position: absolute;
				right: 50rpx;
				bottom: 50rpx;
			}
		}
	}
</style>
